import pkg_resources

from turbogears.widgets import CSSLink, JSLink, Widget, WidgetDescription, \
                               register_static_directory

from gibethemenxsy200901 import release
from gibethemenxsy200901.relativetime import timesince, timeuntil
from gibe.util import utcnow
import cherrypy

resource_name = "gibethemenxsy200901-%s" % (release.version,)
resource_dir = pkg_resources.resource_filename('gibethemenxsy200901', "static")
register_static_directory(resource_name, resource_dir)

class GibeThemeNxsy200901(Widget): 
    version = release.version
    resource_name = resource_name
    css = [
        CSSLink(resource_name, 'css/style.css'),
    ]
    #javascript = [
    #    JSLink(resource_name, 'javascript/domtab.js'),
    #]

    def __init__(self, *args, **kw):
        super(GibeThemeNxsy200901, self).__init__(*args, **kw)
        self._nav = cherrypy.config.get('gibethemenxsy200901.nav', []) 

    def timesince(self, d, now = None):
        if now is None:
            now = utcnow()
        return timesince(d, now)

    def timeuntil(self, d, now = None):
        if now is None:
            now = utcnow()
        return timeuntil(d, now)

    def filter_widgets(self, widgets):
        import gibe.tags.widgets
        for widget in widgets:
            if getattr(widget, "name", None) in ['css/tags.css']:
                continue
            if getattr(widget, "name", None) in ['css/prettify.css']:
                continue
            if getattr(widget, "name", None) in ['javascript/prettify.js']:
                continue
            if getattr(widget, "src", None) in ['prettyPrint()']:
                continue
            if isinstance(widget, gibe.tags.widgets.TagPostDisplayWidget):
                continue
            if isinstance(widget, gibe.tags.widgets.TagDisplayWidget):
                continue
            print widget
            yield widget

    def fib(self, n):
      if n<2:
        return n
      else:
        return self.fib(n-2)+self.fib(n-1)

    @property
    def nav(self):
        for k, v in self._nav:
            yield (k, v)

GibeThemeNxsy200901 = GibeThemeNxsy200901()
